package com.ruoyi.pingan.fymodule.separateSetting.storesetting.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ruoyi.pingan.annotation.CommonLog;
import com.ruoyi.pingan.fymodule.separateSetting.storesetting.entity.FywStoreSetting;
import com.ruoyi.pingan.fymodule.separateSetting.storesetting.entity.ShopInfoSettingRsp;
import com.ruoyi.pingan.fymodule.separateSetting.storesetting.param.*;
import com.ruoyi.pingan.fymodule.separateSetting.storesetting.service.FywStoreSettingService;
import com.ruoyi.pingan.fymodule.thirdpa.util.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

/**
 * 商家分账设置表控制器
 *
 * @author zjf
 * @date  2025/07/29 17:00
 */
@Tag(name = "商家分账设置表控制器")
@RestController
@Validated
public class FywStoreSettingController {

    @Resource
    private FywStoreSettingService fywStoreSettingService;

    /**
     * 获取商家分账设置表分页
     *
     * @author zjf
     * @date  2025/07/29 17:00
     */
    @Operation(summary = "获取商家分账设置表分页")
    @SaCheckPermission("/biz/storesetting/page")
    @GetMapping("/biz/storesetting/page")
    public CommonResult<Page<FywStoreSetting>> page(FywStoreSettingPageParam fywStoreSettingPageParam) {
        return CommonResult.data(fywStoreSettingService.page(fywStoreSettingPageParam));
    }


    /**
     * 获取新增商家信息数据和模板数据
     *
     * @author zjf
     * @date  2025/08/01 09:00
     */
    @Operation(summary = "获取新增商家信息数据和模板数据")
    @SaCheckPermission("/biz/storesetting/new")
    @GetMapping("/biz/storesetting/new")
    public CommonResult<ShopInfoSettingRsp> newShopInfo(FywStoreSettingNewParam fywStoreSettingNewParam) {
        return CommonResult.data(fywStoreSettingService.newShopInfo(fywStoreSettingNewParam));
    }

    /**
     * 添加商家分账设置表
     *
     * @author zjf
     * @date  2025/07/29 17:00
     */
    @Operation(summary = "添加商家分账设置表")
    @CommonLog("添加商家分账设置表")
    @SaCheckPermission("/biz/storesetting/add")
    @PostMapping("/biz/storesetting/add")
    public CommonResult<String> add(@RequestBody @Valid FywStoreSettingAddParam fywStoreSettingAddParam) {

        String storeName = fywStoreSettingAddParam.getStoreName();
        if(storeName==null||storeName==""){
            return CommonResult.error("商家信息为空！");
        }
        if(fywStoreSettingService.checkShop(fywStoreSettingAddParam)){
            fywStoreSettingService.add(fywStoreSettingAddParam);
            return CommonResult.ok();
        }else{
            return CommonResult.error("信息已存在，不允许重复添加！");
        }

    }

    /**
     * 编辑商家分账设置表
     *
     * @author zjf
     * @date  2025/07/29 17:00
     */
    @Operation(summary = "编辑商家分账设置表")
    @CommonLog("编辑商家分账设置表")
    @SaCheckPermission("/biz/storesetting/edit")
    @PostMapping("/biz/storesetting/edit")
    public CommonResult<String> edit(@RequestBody @Valid FywStoreSettingEditParam fywStoreSettingEditParam) {
        fywStoreSettingService.edit(fywStoreSettingEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除商家分账设置表
     *
     * @author zjf
     * @date  2025/07/29 17:00
     */
    @Operation(summary = "删除商家分账设置表")
    @CommonLog("删除商家分账设置表")
    @SaCheckPermission("/biz/storesetting/delete")
    @PostMapping("/biz/storesetting/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<FywStoreSettingIdParam> fywStoreSettingIdParamList) {
        fywStoreSettingService.delete(fywStoreSettingIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取商家分账设置表详情
     *
     * @author zjf
     * @date  2025/07/29 17:00
     */
    @Operation(summary = "获取商家分账设置表详情")
    @SaCheckPermission("/biz/storesetting/detail")
    @GetMapping("/biz/storesetting/detail")
    public CommonResult<FywStoreSetting> detail(@Valid FywStoreSettingIdParam fywStoreSettingIdParam) {
        return CommonResult.data(fywStoreSettingService.detail(fywStoreSettingIdParam));
    }

    /**
     * 下载商家分账设置表导入模板
     *
     * @author zjf
     * @date  2025/07/29 17:00
     */
    @Operation(summary = "下载商家分账设置表导入模板")
    @GetMapping(value = "/biz/storesetting/downloadImportTemplate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadImportTemplate(HttpServletResponse response) throws IOException {
        fywStoreSettingService.downloadImportTemplate(response);
    }

    /**
     * 导入商家分账设置表
     *
     * @author zjf
     * @date  2025/07/29 17:00
     */
    @Operation(summary = "导入商家分账设置表")
    @CommonLog("导入商家分账设置表")
    @SaCheckPermission("/biz/storesetting/importData")
    @PostMapping("/biz/storesetting/importData")
    public CommonResult<JSONObject> importData(@RequestPart("file") MultipartFile file) {
        return CommonResult.data(fywStoreSettingService.importData(file));
    }

    /**
     * 导出商家分账设置表
     *
     * @author zjf
     * @date  2025/07/29 17:00
     */
    @Operation(summary = "导出商家分账设置表")
    @SaCheckPermission("/biz/storesetting/exportData")
    @PostMapping(value = "/biz/storesetting/exportData", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportData(@RequestBody List<FywStoreSettingIdParam> fywStoreSettingIdParamList, HttpServletResponse response) throws IOException {
        fywStoreSettingService.exportData(fywStoreSettingIdParamList, response);
    }
}
